<template>
  <div>
    <el-card>
      <el-form ref="editFormRef" :model="editForm" :rules="rules" label-width="80px" class="Login_from">
        <el-form-item label="旧的密码" prop="password">
          <el-input v-model="editForm.password"  type="password" ></el-input>
        </el-form-item>
        <el-form-item label="新的密码"  prop="newPassword1">
          <el-input v-model="editForm.newPassword1" type="password"  ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="newPassword">
          <el-input v-model="editForm.newPassword" type="password" ></el-input>
        </el-form-item>
        <el-form-item class="btn">
          <el-button type="primary" @click="resetPassword">修改密码</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (this.editForm.password !== '') {
          this.$refs.editFormRef.validateField('checkPass')
        }
        callback()
      }
    }
    var validatePass2 = (rule, value, callback) => {
      // console.log(value)
      if (value === '') {
        callback(new Error('不能为空'))
      } else if (value !== this.editForm.newPassword1) {
        callback(new Error(('两次输入密码不一致!')))
      } else {
        callback()
      }
    }
    return {
      password: '',
      residentInfo: {},
      Info: {},
      editForm: {
        id: window.sessionStorage.getItem('userId'),
        password: '',
        newPassword1: '',
        newPassword: '',
        identity: 2
      },
      rules: {
        newPassword: [{ validator: validatePass2, trigger: 'blur' }],
        password: [{ required: true, min: 5, max: 14, message: '请输入密码', trigger: 'blur' },
          { validator: validatePass, trigger: 'blur' }],
        newPassword1: [{ required: true, message: '请输入新的密码', trigger: 'blur' },
          { min: 5, max: 14, message: '长度在 5 到 14 个字符', trigger: 'blur' }]
      }
    }
  },
  created () {
  },
  methods: {
    resetPassword () {
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) return
        const result = await this.$http.post('resetPassword', this.editForm)
        if (!result) { return this.$message.error('修改失败') }
        this.$message.success('修改成功')
        this.$router.push('/home')
      }
      )
    },
    resetLoginForm () {
      this.$refs.editFormRef.resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
</style>
